import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'ThemeUtil.dart';

BoxShadow haoBoxShadow(
    {double blurRadius = 10, Offset offset = const Offset(1, 1)}) {
  return BoxShadow(
      color: Get.theme.shadowColor, blurRadius: blurRadius, offset: offset);
}

TextField zuTextField({
  required TextEditingController controller,
  bool obscureText = false,
  String? hintText,
  TextInputType? inputType,
}) {
  return TextField(
    controller: controller,
    style: const TextStyle(fontSize: 16),
    keyboardType: inputType,
    decoration: InputDecoration(
        border: InputBorder.none,
        hintText: hintText,
        hintStyle: const TextStyle(color: Colors.grey)),
    obscureText: obscureText,
  );
}

showAlert(String title, String desc, VoidCallback callback) {
  showCupertinoDialog(
      context: Get.context!,
      builder: (context) {
        return CupertinoAlertDialog(
          title: Text(title),
          content: Text(desc),
          actions: [
            CupertinoDialogAction(
              child: const Text('取消'),
              onPressed: () {
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: const Text('确定'),
              onPressed: () {
                callback();
              },
            ),
          ],
        );
      });
}

Future<void> configMode() async {
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  int mode = sharedPreferences.getInt("mode") ?? 0;
  if (mode == 0) {
    if (isDarkMode()) {
      Get.changeThemeMode(ThemeMode.dark);
      Get.changeTheme(TTheme.nightTheme);
    } else {
      Get.changeThemeMode(ThemeMode.light);
      Get.changeTheme(TTheme.dayTheme);
    }
  } else if (mode == 1) {
    // 关闭
    Get.changeThemeMode(ThemeMode.light);
    Get.changeTheme(TTheme.dayTheme);
  } else {
    // 暗黑
    Get.changeThemeMode(ThemeMode.dark);
    Get.changeTheme(TTheme.nightTheme);
  }

  Future.delayed(const Duration(milliseconds: 300),() {
    Get.forceAppUpdate();
  });
}

bool isDarkMode() {
  bool isDark = false;
  try {
    isDark = (window.platformBrightness == Brightness.dark);
  } catch (_) {}
  return isDark;
}
